<template>
  <WsmTable
    ref="table"
    load-url="/rbac-server/rbac/roletype/page"
    :change-load="true"
    v-model="queryFormType"
  >
    <template #title>{{ t('rbac.roletype.title') }}</template>
    <template #query="{ data }">
      <el-form-item :label="t('rbac.roletype.rState')">
        <el-radio-group v-model="data.rState.value">
          <el-radio value="">{{ t('rbac.roletype.all') }}</el-radio>
          <el-radio value="0">{{ t('rbac.roletype.disable') }}</el-radio>
          <el-radio value="1">{{ t('rbac.roletype.enable') }}</el-radio>
        </el-radio-group>
      </el-form-item>
    </template>
    <template #button="{ row }">
      <el-button type="primary" v-hasPermi="'add'" @click="addForm.show()">{{
        t('common.add')
      }}</el-button>
      <template v-if="row">
        <el-button v-hasPermi="'update'" @click="update(row.code)" type="primary">{{
          t('common.update')
        }}</el-button>
        <el-button v-hasPermi="'delete'" @click="del(row.code)" type="danger">{{
          t('common.delete')
        }}</el-button>
        <el-button v-hasPermi="'sort'" @click="sort(row.code, 1)" type="success">{{
          t('rbac.roletype.up')
        }}</el-button>
        <el-button v-hasPermi="'sort'" @click="sort(row.code, 0)" type="info">{{
          t('rbac.roletype.down')
        }}</el-button>
        <el-button v-hasPermi="'bind'" @click="bind(row.code)" type="warning">{{
          t('common.bind')
        }}</el-button>
      </template>
    </template>
    <template #column>
      <el-table-column prop="rState" :label="t('rbac.roletype.rState')" width="70">
        <template #default="scope">
          <template v-if="scope.row.rState">
            <el-tag effect="dark" type="success">{{ t('rbac.roletype.enable') }}</el-tag>
          </template>
          <template v-else>
            <el-tag effect="dark" type="danger">{{ t('rbac.roletype.disable') }}</el-tag>
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="code" :label="t('rbac.roletype.code')" width="180" />
      <el-table-column prop="typeName" :label="t('rbac.roletype.typeName')" width="180" />
      <el-table-column prop="note" :label="t('rbac.roletype.note')" />
    </template>
  </WsmTable>
  <WsmForm
    ref="addForm"
    v-model="addData"
    submit-url="/rbac-server/rbac/roletype/add"
    label-width="100px"
    :init-data="initData"
    :title="t('rbac.roletype.addTitle')"
    @success="flushed"
  >
    <template #default="{ form }"
      ><el-row>
        <el-col :span="24">
          <el-form-item prop="code" :label="t('rbac.roletype.code')">
            <el-input v-model="form.code" :placeholder="t('rbac.roletype.code')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item prop="typeName" :label="t('rbac.roletype.typeName')">
            <el-input v-model="form.typeName" :placeholder="t('rbac.roletype.typeName')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="rState" :label="t('rbac.roletype.rState')">
            <el-radio-group v-model="form.rState">
              <el-radio value="0">{{ t('rbac.roletype.disable') }}</el-radio>
              <el-radio value="1">{{ t('rbac.roletype.enable') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item prop="note" :label="t('rbac.roletype.note')">
            <el-input type="textarea" v-model="form.note" :placeholder="t('rbac.roletype.note')" />
          </el-form-item>
        </el-col>
      </el-row>
    </template>
  </WsmForm>

  <WsmForm
    ref="updateForm"
    v-model="updateData"
    load-url="/rbac-server/rbac/roletype/get"
    label-width="100px"
    submit-url="/rbac-server/rbac/roletype/update"
    :title="t('rbac.roletype.updateTitle')"
    @success="flushed"
  >
    <template #default="{ form }">
      <el-row>
        <el-col :span="12">
          <el-form-item prop="typeName" :label="t('rbac.roletype.typeName')">
            <el-input v-model="form.typeName" :placeholder="t('rbac.roletype.typeName')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="rState" :label="t('rbac.roletype.rState')">
            <el-radio-group v-model="form.rState">
              <el-radio :value="false">{{ t('rbac.roletype.disable') }}</el-radio>
              <el-radio :value="true">{{ t('rbac.roletype.enable') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item prop="note" :label="t('rbac.roletype.note')">
            <el-input type="textarea" v-model="form.note" :placeholder="t('rbac.roletype.note')" />
          </el-form-item>
        </el-col>
      </el-row>
    </template>
  </WsmForm>

  <WsmForm
    ref="bindForm"
    v-model="bindData"
    load-url="/rbac-server/rbac/resource/getResourceRoleType"
    label-position="top"
    submit-url="/rbac-server/rbac/resource/updateResourceRoleType"
    :title="t('rbac.bindTitle')"
  >
    <template #default="{ form }">
      <el-form-item prop="resId" :label="t('rbac.bindResource')">
        <el-checkbox-group v-model="form.resId">
          <el-checkbox
            v-for="item in resources"
            :key="item.id"
            :label="item.resName"
            :value="item.id"
          >
            {{ item.resName }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </template>
  </WsmForm>
</template>
<script setup lang="ts">
import { WsmTable } from '@/components/WsmTable'
import { WsmForm } from '@/components/WsmForm'
import { QueryFormType, QueryType } from '@/components/WsmTable/src/types'
import { delApi, sortApi } from '@/api/rbac/roletype'
import { ref } from 'vue'
import { ElMessageBox } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { FormDataType } from '@/components/WsmForm/src/types'
import { ResourceType } from '@/api/rbac/resource/types'
import { listResourceApi } from '@/api/rbac/resource'

const { t } = useI18n()

//表格对象
const table = ref()

//新增表单对象
const addForm = ref()
const initData = ref({
  rState: '1'
})
const addData: FormDataType = {
  code: '',
  typeName: '',
  note: '',
  rState: '1'
}

//修改表单对象
const updateForm = ref()
const updateData: FormDataType = {
  code: '',
  typeName: '',
  note: '',
  rState: '1'
}

//查询定义
const queryFormType: QueryFormType = {
  rState: {
    value: '',
    queryType: QueryType.EQ
  }
}

//绑定表单对象
const bindForm = ref()
const bindData: FormDataType = {
  code: '',
  resId: []
}
const bind = (code: string) => {
  bindForm.value.show({ code })
}

const resources = ref<ResourceType[]>([])

//加载可以配置的资源
const loadResource = async () => {
  const rs = await listResourceApi()
  if (rs.success) {
    resources.value = rs.data
  }
}
loadResource()

const update = (code: string) => {
  updateForm.value.show({ code })
}
const del = async (code: string) => {
  ElMessageBox.confirm(t('common.delMessage'), t('common.delWarning'), {
    confirmButtonText: t('common.ok'),
    cancelButtonText: t('common.cancel'),
    type: 'warning'
  }).then(async () => {
    const rs = await delApi(code)
    if (rs.success) {
      table.value.load(1)
    }
  })
}

const sort = async (code: string, type: number) => {
  const rs = await sortApi({ code, type })
  if (rs.success) {
    table.value.load()
  }
}

const flushed = () => {
  table.value.load(1)
}
</script>

<style scoped></style>
